package org.zachary.agent.infrastructure.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import org.zachary.agent.domain.aggregate_model.user_credit.UserCredit;

public interface SpringDataUserCreditRepository extends JpaRepository<UserCredit,Integer> {
    @Modifying(clearAutomatically = true)
    @Query("UPDATE UserCredit uc SET " +
            "uc.creditScore=:#{#u.creditScore},"+
            "uc.accountType=:#{#u.accountType}," +
            "uc.idNumber=:#{#u.idNumber}," +
            "uc.annualIncome=:#{#u.annualIncome}," +
            "uc.qualification=:#{#u.qualification}," +
            "uc.jobType=:#{#u.jobType}," +
            "uc.maritalStatus=:#{#u.maritalStatus} ," +
            "uc.updateTime=CURRENT_TIMESTAMP ,"+
            "uc.idCardFront=:#{#u.idCardFront}," +
            "uc.idCardBack=:#{#u.idCardBack},"+
            "uc.workProof=:#{#u.workProof}," +
            "uc.educationProof=:#{#u.educationProof}," +
            "uc.incomeProof=:#{#u.incomeProof} "+
            "WHERE uc.id=:id")
    void updateById(@Param("id")int id,@Param("u")UserCredit u);

    @Modifying
    @Query("UPDATE UserCredit uc SET " +
            "uc.status=3,"+
            "uc.updateTime=CURRENT_TIMESTAMP "+
            "WHERE uc.id=:userId")
    void setStatusToPassed(@Param("userId") Integer userId);

    @Modifying
    @Query("UPDATE UserCredit uc SET " +
            "uc.status=2,"+
            "uc.updateTime=CURRENT_TIMESTAMP "+
            "WHERE uc.id=:userId")
    void setStatusToNotPassed(@Param("userId") Integer userId);

    @Modifying
    @Transactional
    @Query("UPDATE UserCredit uc SET " +
            "uc.status=1,"+
            "uc.updateTime=CURRENT_TIMESTAMP "+
            "WHERE uc.id=:userId")
    void setInfoInImproving(@Param("userId") Integer userId);
}
